<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Http\Requests\finance\TransferGetRequest;
use App\Models\finance\TransferGetModel;
use App\Models\TakeOrderModel;
use Illuminate\Http\Request;

class TransferGet extends Controller
{
    use TransferGetRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $customer_name = $request->input('customer_name', '');
        $account_id = $request->input('account_id', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');
        $creator = $request->input('creator', '');

        $query = TransferGetModel::query()->with(['account'])->withWhereHas('customer', function ($query) use ($customer_name) {
            if($customer_name) {
                $query->where('name', 'like', "%$customer_name%");
            }
        })->withWhereHas('create_admin', function ($query) use ($creator) {
            if($creator) {
                $query->withWhereHas('staff', function ($query) use ($creator){
                    $query->where('name', 'like', "%$creator%");
                });
            }
        });

        if($account_id){
            $query->where('account_id', $account_id);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $total_amount = $query->sum('amount');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'customer_name' => $item->customer ? $item->customer->name : '',
                'customer_sn' => $item->customer ? $item->customer->sn : '',
                'account_name' => $item->account ? $item->account->name : '',
                'bank_name' => $item->account ? $item->account->bank_name : '',
                'bank_number' => $item->account ? $item->account->bank_number : '',
                'amount' => $item->amount,
                'remarks' => $item->remarks,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name : '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total, 'total_amount' => $total_amount]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $take_order_sn = $params['take_order_sn'] ?? [];

        foreach ($take_order_sn as $sn) {
            $find = TakeOrderModel::where('sn', $sn)->first();
            if(!$find){
                return $this->error('提单号【 '.$sn.'】不存在');
            }
        }

        $model = new TransferGetModel();
        $model->customer_id = $params['customer_id'];
        $model->account_id = $params['account_id'];
        $model->take_order_sn = json_encode($params['take_order_sn']);
        $model->amount = $params['amount'];
        $model->files = json_encode($params['files']);
        $model->remarks = $params['remarks'] ?? '';
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

}
